package com.etoak.web.sys_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etoak.web.sys_menu.entity.AssignTreeParm;
import com.etoak.web.sys_menu.entity.AssignTreeVo;
import com.etoak.web.sys_menu.entity.MakeMenuTree;
import com.etoak.web.sys_menu.entity.SysMenu;
import com.etoak.web.sys_menu.service.SysMenuService;
import com.etoak.web.sys_role_user.entity.SysUserRole;
import com.etoak.web.sys_role_user.service.SysUserRoleService;
import com.etoak.web.sys_user.entity.SysUser;
import com.etoak.web.sys_user.mapper.SysUserMapper;
import com.etoak.web.sys_user.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author by 孙有泽
 * @Date 2025/2/18 12:53
 * @Description
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysMenuService sysMenuService;
    @Transactional
    @Override
    public void saveUser(SysUser sysUser) {
        int i=this.baseMapper.insert(sysUser);
        if(i>0){
            String[] split = sysUser.getRoleId().split(",");
            List<SysUserRole> roles=new ArrayList<>();
            System.out.println(split);
            if(split.length>0){
                for (String s : split) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRole.setRoleId((s));
                    roles.add(sysUserRole);
                }
            }
            sysUserRoleService.saveBatch(roles);
        }
    }

    @Override
    @Transactional
    public void updateUser(SysUser sysUser) {
        int i=this.baseMapper.updateById(sysUser);

        if(i>0){
            QueryWrapper<SysUserRole> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("user_id",sysUser.getId());
            sysUserRoleService.remove(objectQueryWrapper);
            String[] split = sysUser.getRoleId().split(",");
            List<SysUserRole> roles=new ArrayList<>();
            if(split.length>0){
                for (String s : split) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRole.setRoleId((s));
                    roles.add(sysUserRole);
                }
            }
            sysUserRoleService.saveBatch(roles);
        }
    }

    @Override
    @Transactional
    public void deleteUser(String userId) {
        int i=this.baseMapper.deleteById(userId);
        if(i>0){
            QueryWrapper<SysUserRole> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("user_id",userId);
            sysUserRoleService.remove(objectQueryWrapper);

        }

    }

    @Override
    public AssignTreeVo getAssignTree(AssignTreeParm parm) {
        //查询用户的信息
        List<SysMenu> menuList=null;
        SysUser byId = this.baseMapper.selectById(parm.getUserId());
        if(StringUtils.isNotEmpty(byId.getIsadmin()+"")&& "0".equals(byId.getIsadmin()+"")){
            menuList=sysMenuService.list();
        }else{
            menuList=sysMenuService.queryMenuByUserId(parm.getUserId());
        }
        List<SysMenu> makeTree = MakeMenuTree.makeTree(menuList, "0");

        List<SysMenu> sysMenus = sysMenuService.queryMenuByRoleId(parm.getRoleId());
        ArrayList<String> objects = new ArrayList<>();
        Optional.ofNullable(sysMenus).orElse(new ArrayList<>())
                .stream().filter(item->item!=null).forEach(item->{
                    objects.add(item.getId());
                });
        AssignTreeVo assignTreeVo = new AssignTreeVo();
        assignTreeVo.setCheckList(objects.toArray());
        assignTreeVo.setListmenu(makeTree);
        return assignTreeVo;
    }

    @Override
    public SysUser loadUserByUsername(String username) {
        QueryWrapper<SysUser> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.lambda().eq(SysUser::getUsername,username);
        SysUser sysUser = this.baseMapper.selectOne(objectQueryWrapper);
        return sysUser;
    }
}
